import Model from '../abstract';
import ModelCatalogOptionValue from './option/value';

export default class ModelCatalogOption extends Model {

      constructor() {
            super();
            this.mainTable = 'product_option';
            this.pk = 'option_id';
            this.serverPk = 'server_option_id';
            this.modelOptionValue = new ModelCatalogOptionValue();
      }

      /**
       * 获取option
       */
      async getOption(optionId) {
            let option = await this.get(optionId);
            if (option) {
                  option.values = await this.modelOptionValue.getValues(optionId);
            }
            return option;
      }

      /**
       * 获取options
       */
      async getOptions(productId) {
            let options = await this.fetchRows("SELECT * FROM `product_option` WHERE `product_id` = ? ORDER BY `sort_order` ASC", [productId]);
            for (option of options) {
                  let optionId = option.option_id;
                  option.values = await this.modelOptionValue.getValues(optionId);
            }
            return options;
      }

      /**
       * 保存商品options
       */
      async saveOptions(productId, options = []) {
            if (productId > 0) {
                  if (!Array.isArray(options)) {
                        options = [];
                  }
                  for (option of options) {
                        await this.saveOption(productId, option);
                  }
            }
      }

      /**
       * 保存商品option
       */
      async saveOption(productId, option) {
            let optionId = 0;
            if (productId > 0 && typeof(option) == 'object') {
                  let values = {};
                  if (option.values) {
                        values = option.values;
                        delete option.values;
                  }
                  if(Object.keys(values).length > 0) {
                        option.product_id = productId;
                        optionId = parseInt(option.option_id);
                        optionId = await this.save(option, optionId);
                        if (optionId > 0) {
                              await this.modelOptionValue.saveValues(optionId, values);
                        }
                  }
            }
            return optionId;
      }

      /**
       * 保存商品options
       */
      async saveOptionsByServerPk(productId, options = []) {
            if (productId > 0) {
                  if (!Array.isArray(options)) {
                        options = [];
                  }
                  for (option of options) {
                        await this.saveOptionByServerPk(productId, option);
                  }
            }
      }

      /**
       * 保存商品option
       */
      async saveOptionByServerPk(productId, option) {
            let optionId = 0;
            if (productId > 0 && typeof(option) == 'object') {
                  let values = {};
                  if (option.values) {
                        values = option.values;
                        delete option.values;
                  }
                  if(Object.keys(values).length > 0) {
                        option.product_id = productId;
                        serverOptionId = parseInt(option.server_option_id);
                        optionId = await this.saveByServerPk(option, serverOptionId);
                        if (optionId > 0) {
                              await this.modelOptionValue.saveValuesByServerId(optionId, values);
                        }
                  }
            }
            return optionId;
      }

      /**
       * 按照optionId删除
       */
      async removeOptions(productId) {
            let options = await this.getOptions(productId);
            for (option of options) {
                  let optionId = option.option_id;
                  await this.modelOptionValue.removeValues(optionId);
                  await this.remove(optionId);
            }
      }
}